﻿using System;
using System.Windows.Forms;
using System.Data;
using System.Linq;
//using SManage.SalesFrm;
//using SManage.StorageFrm;
using System.Data.SqlClient;
using SManage.Model;
using System.Configuration;
namespace SManage
{   
    public partial class MainFrm : Form
    {
        string Conn = @"Data Source=.\sqlexpress;Initial Catalog=wlaq;Integrated Security=True";
        public MainFrm()
        {
            InitializeComponent();
        }
        public bool LoginOK = false;
        private void MainFrm_Load(object sender, EventArgs e)
        {
            //LoginSys();      
            dtp_DPInDate.Value = DateTime.Now.Date; 
        }
        public string OperatorCode = "";
        /// <summary>
        /// 根据用户名称判断权限表
        /// </summary>
        /// <param name="OperatorCode"></param>
        /*public void getPriority( )
        {
            //判断常用工具
            int i = 0;
            foreach (ToolStripDropDownItem item in menu_tool.DropDownItems)
            {
                if (i == 2)
                {
                    item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                    btb_Menu.Groups[0].Items[i].Enabled = item.Enabled;
                    i++;
                }
            }
            //判断采购
            i = 0;
            foreach (ToolStripDropDownItem item in menu_Stock.DropDownItems)
            {               
                item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                btb_Menu.Groups[1].Items[i].Enabled = item.Enabled;
                i++;
            }
            //判断销售　
            i = 0;
            foreach (ToolStripDropDownItem item in menu_Sales.DropDownItems)
            {
                item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                btb_Menu.Groups[2].Items[i].Enabled = item.Enabled;
                i++;
            }
            //判断库存
            i = 0;
            foreach (ToolStripDropDownItem item in menu_Storage.DropDownItems)
            {
                item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                btb_Menu.Groups[3].Items[i].Enabled = item.Enabled;
                i++;
            }
            //判断基础资料
            i = 0;
            foreach (ToolStripDropDownItem item in menu_Basic.DropDownItems)
            {
                item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                btb_Menu.Groups[4].Items[i].Enabled = item.Enabled;
                i++;
            }
            //系统库存
            i = 0;
            foreach (ToolStripDropDownItem item in menu_System.DropDownItems)
            {
                item.Enabled = UserPriority.getPriority(PublicItem.operatorItem.OperatorCode, item.Name);
                btb_Menu.Groups[5].Items[i].Enabled = item.Enabled;
                i++;
            }
        }*/

        #region 采购模块事件函数
        private void InquiriesProcurementPlans_Click(object sender, EventArgs e)
        {
            //采购计划查询
          //  StockPlanQueryFrm queryFrm = new StockPlanQueryFrm(false);
           // queryFrm.ShowDialog();
        }

        private void SingleVendorRegistration_Click(object sender, EventArgs e)
        {
            //供应商登记单
            //供应商资料
           // SystemFrm.SupliersFrm stock_Supliers = new SupliersFrm();
           // stock_Supliers.ShowDialog();
        }

        private void DetailedSupplyVendors_Click(object sender, EventArgs e)
        {
            //供应商供货明细
            //供应商资料
           // SystemFrm.SupliersFrm stock_Supliers_mx = new SupliersFrm();
            //stock_Supliers_mx.ShowDialog();
        }
        #endregion

        private void ProcurementOfGoods_Click(object sender, EventArgs e)
        {
            //采购退货单
          //  StorageFrm.StorageInFrm pogf = new SManage.StorageFrm.StorageInFrm(StorageOperateType.OutStorage);
           // pogf.ShowDialog();
        }

        private void ProcureOfGoodsInquiry_Click(object sender, EventArgs e)
        {
            //采购退货查询
           // StorageFrm.StorageInQueryFrm pogif = new SManage.StorageFrm.StorageInQueryFrm(StorageOperateType.InStorage);
           // pogif.ShowDialog();
        }        
        /// <summary>
        /// 注销或者登陆
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void LoginSys()
        {
            //LoginFrm fr = new LoginFrm(this,WindowType.strCancellation);
            //fr.ShowDialog();
        }
        //按钮工具条相应函数
        //private void buttonBar1_ItemClick(object sender, Janus.Windows.ButtonBar.ItemEventArgs e)
        //{
            
        //}

        private void tsm_Lock_Click(object sender, EventArgs e)
        {
            SManage.LocksFrm lc = new LocksFrm();
            lc.ShowDialog();
        }
       
        private void Menu_SalesOutStorage_Click(object sender, EventArgs e)
        {
            //销售出库单            
           // SalesOutStorageFrm sfp = new SalesOutStorageFrm();
           // sfp.ShowDialog();
        }

        private void Menu_SalesOffGoods_Click(object sender, EventArgs e)
        {
            //销售退货单
           // SalesOffGoodsFrm sog = new SalesOffGoodsFrm();
           // sog.ShowDialog();
        }

        private void Menu_SalesValidateAlarm_Click(object sender, EventArgs e)
        {
            //近效期提示单
           // StorageValidateAlarmFrm queryFrm = new StorageValidateAlarmFrm(WindowType.SalesValidateWnd);
           // queryFrm.ShowDialog();
        }

        private void Menu_SalesLimitAlarm_Click(object sender, EventArgs e)
        {
            //销售库存预警
          //  StorageLimitAlarmFrm queryFrm = new StorageLimitAlarmFrm(WindowType.SalesLimitWnd);
           // queryFrm.ShowDialog();
        }

        private void Menu_SalesOutStorageQuery_Click(object sender, EventArgs e)
        {
            //销售出库单查询
          ///  SalesOutStorageQueryFrm queryFrm = new SalesOutStorageQueryFrm();
          //  queryFrm.ShowDialog();
        }

        private void Menu_SalesOffGoodsQuery_Click(object sender, EventArgs e)
        {
            //销售退货单查询
          //  SalesOffGoodsQueryFrm queryFrm = new SalesOffGoodsQueryFrm();
          //  queryFrm.ShowDialog();
        }

        private void Menu_StorageCheckQuery_Click(object sender, EventArgs e)
        {
            //盘点单查询
          //  StorageFrm.StorageCheckQueryFrm iif = new SManage.StorageFrm.StorageCheckQueryFrm();
          //  iif.ShowDialog();
        }

        private void Menu_StorageCheck_Click(object sender, EventArgs e)
        {
            //盘点登记单
          //  StorageFrm.StorageCheckFrm scf = new SManage.StorageFrm.StorageCheckFrm();
           // scf.ShowDialog();
        }

        private void Menu_StorageDetailQuery_Click(object sender, EventArgs e)
        {
            //库存明细查询
           // StorageFrm.StorageDetailQueryFrm idf = new SManage.StorageFrm.StorageDetailQueryFrm();
          //  idf.ShowDialog();
        }

        private void Menu_StorageAdjustPrice_Click(object sender, EventArgs e)
        {
            //存货调价管理
          //  StorageFrm.StorageAdjustPriceFrm aidf = new SManage.StorageFrm.StorageAdjustPriceFrm();
           // aidf.ShowDialog();
        }

        private void Menu_StorageLimitAlarm_Click(object sender, EventArgs e)
        {
            //库存上下限报警
          //  StorageLimitAlarmFrm queryFrm = new StorageLimitAlarmFrm(WindowType.StorageLimitWnd);
          //  queryFrm.ShowDialog();
        }

        private void Menu_StorageIn_Click(object sender, EventArgs e)
        {
            //采购入库验收单
          //  StorageFrm.StorageInFrm pcf = new SManage.StorageFrm.StorageInFrm(StorageOperateType.InStorage);
          //  pcf.ShowDialog();
        }

        private void Menu_StorageValidateAlarm_Click(object sender, EventArgs e)
        {
            //库存近效期报警
          //  StorageFrm.StorageValidateAlarmFrm svaf = new SManage.StorageFrm.StorageValidateAlarmFrm(WindowType.StorageValidateWnd);
          //  svaf.ShowDialog();
        }

        private void Menu_StockPlan_Click(object sender, EventArgs e)
        {
            //登记采购计划单
         //   StorageFrm.StockPlanFrm ppf = new SManage.StorageFrm.StockPlanFrm();
         //   ppf.ShowDialog();
        }

        private void Menu_StockPlanQuery_Click(object sender, EventArgs e)
        {
            //采购计划查询
           // StorageFrm.StockPlanQueryFrm ippf = new SManage.StorageFrm.StockPlanQueryFrm(false);
           // ippf.ShowDialog();
        }

        private void Menu_StorageInQuery_Click(object sender, EventArgs e)
        {
            //采购入库单查询
           // StorageFrm.StorageInQueryFrm ipdf = new SManage.StorageFrm.StorageInQueryFrm(StorageOperateType.InStorage);
           // ipdf.ShowDialog();
        }

        private void Menu_StockPlanCheck_Click(object sender, EventArgs e)
        {
            //审核采购计划
           // SManage.StorageFrm.StockPlanCheckFrm spck = new SManage.StorageFrm.StockPlanCheckFrm();
           // spck.ShowDialog();
        }

        private void Menu_StorageDispatch_Click(object sender, EventArgs e)
        {
            //调拨单
          //  SManage.StorageFrm.StorageDispatchFrm sd = new SManage.StorageFrm.StorageDispatchFrm();
          //  sd.ShowDialog();
        }

        private void Menu_StorageDispatchQuery_Click(object sender, EventArgs e)
        {
            //调拨单查询
          //  SManage.StorageFrm.StorageDispatchQueryFrm sdq = new StorageDispatchQueryFrm();
           // sdq.ShowDialog();
        }

        private void Menu_StorageOut_Click(object sender, EventArgs e)
        {
            //采购退货单
            //StorageFrm.StorageInFrm StorageOut = new SManage.StorageFrm.StorageInFrm(StorageOperateType.OutStorage);            
            //StorageOut.ShowDialog();
          //  StorageFrm.StorageOutFrm StorageOut = new StorageOutFrm();
          //  StorageOut.ShowDialog();
        }

        private void Menu_StorageOutQuery_Click(object sender, EventArgs e)
        {
            //采购退货查询
          //  StorageFrm.StorageInQueryFrm StorageOutQuery = new SManage.StorageFrm.StorageInQueryFrm(StorageOperateType.OutStorage);
          //  StorageOutQuery.ShowDialog();
        }

        private void Menu_Staff_Click(object sender, EventArgs e)
        {
            //员工资料
          //  SystemFrm.Staff staff = new Staff();
           // staff.ShowDialog();
        }

        private void Menu_Deptartment_Click(object sender, EventArgs e)
        {
            //部门资料
           // SystemFrm.DepartmentFrm df = new DepartmentFrm();
           // df.ShowDialog();
        }

        private void Menu_Supplier_Click(object sender, EventArgs e)
        {
            //供应商资料
           // SystemFrm.SupliersFrm sf = new SupliersFrm();
           // sf.ShowDialog();
        }

        private void Menu_Drugs_Click(object sender, EventArgs e)
        {
            //药品资料
           // SystemFrm.Drug drug = new Drug();
           // drug.ShowDialog();
        }

        private void Menu_Area_Click(object sender, EventArgs e)
        {
            //地区
           // SystemFrm.AreaFrm area = new AreaFrm();
           // area.ShowDialog();
        }

        private void Menu_StorageOper_Click(object sender, EventArgs e)
        {
            //仓库
          ///  SystemFrm.StoreHouse sh = new StoreHouse();
          //  sh.ShowDialog();
        }

        private void Menu_Basical_Click(object sender, EventArgs e)
        {
            //基本设置
          //  SystemFrm.BasicFrm bf = new BasicFrm();
          //  bf.ShowDialog();
        }

        private void Menu_StorageInitialize_Click(object sender, EventArgs e)
        {
            //期初管理
          //  SManage.SystemFrm.StorageInitializeFrm qichu = new StorageInitializeFrm();
          //  qichu.ShowDialog();
        }

        private void Menu_DB_Click(object sender, EventArgs e)
        {
            //数据管理            
          //  DBoperateFrm db = new DBoperateFrm();
          //  db.ShowDialog();
        }

        private void Menu_Pwd_Click(object sender, EventArgs e)
        {
            //修改密码
          //  SystemFrm.AlterPwdFrm apwd = new AlterPwdFrm();
          //  apwd.ShowDialog();
        }

        private void Menu_Power_Click(object sender, EventArgs e)
        {
            //权限设置
          //  SystemFrm.PowerManagement pm = new PowerManagement();
          //  pm.ShowDialog();
        }

        private void Menu_Print_Click(object sender, EventArgs e)
        {
            //打印机设置
          //  SystemFrm.PrintInstalledFrm print = new PrintInstalledFrm();
          //  print.ShowDialog();
        }

        private void Menu_about_Click(object sender, EventArgs e)
        {
         //   SystemFrm.about about = new about();
         //   about.ShowDialog();
        }

        private void Menu_toolCalculator_Click(object sender, EventArgs e)
        {
            //计算器
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void Menu_toolCancellation_Click(object sender, EventArgs e)
        {
            //注销
            //PublicItem.operatorItem = null;
            //WindowType.strCancellation = "0";
            //LoginSys();
        }
        private void pbox_main_Click(object sender, EventArgs e)
        {

        }

        private void Menu_SalesProfit_Click(object sender, EventArgs e)
        {
            //销售利润
           // SalesFrm.SalesProfitFrm toolSalesProfit = new SalesProfitFrm();
            //toolSalesProfit.ShowDialog();
        }

        private void Menu_SalesShift_Click(object sender, EventArgs e)
        {
            //交班管理
           // Sales.SalesShiftDetailFrm toolshift = new SManage.Sales.SalesShiftDetailFrm();
           // toolshift.ShowDialog();
        }

        private void Menu_Log_Click(object sender, EventArgs e)
        {

        }

        private void mIBToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //frmStudyMib sb = new frmStudyMib();
            //sb.ShowDialog();
        }

        private void bnPrint_Click(object sender, EventArgs e)
        {

        }    
        //===========================================================设施信息管理
        /// <summary>
        /// 显示设施基本信息，包括IP，和物理方位信息
        /// </summary>
        private void ShowDeviceBaseInf()
        {
           
           DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from device in Devices.Devices
                         join DevicePosition in Devices.T_DevicePosition on device.DeviceId equals DevicePosition.iDeviceId into MainDevice
                         from RightMain in MainDevice.DefaultIfEmpty()
                         select new { 设备编号 = device.DeviceId, /*端口 = device.interfaces, IP地址 = device.ip,*/ 单位名称 =RightMain==null?"":RightMain.sUnitName,
                                      楼号 = RightMain == null ? "" : RightMain.sBuildingNo,
                                      楼层号 = RightMain == null ? "" : RightMain.iFloor.ToString(),
                                      房间号 = RightMain == null ? "" : RightMain.sRoomNo,
                                      房内号 = RightMain == null ? "" : RightMain.sNoInRoom,
                                      使用者姓名 = RightMain == null ? "" : RightMain.sUserName.Trim(),
                                      备注 = RightMain == null ? "" : RightMain.sRemark.Trim()

                         };
            dgvShowMainDevice.DataSource = result; 
        }
        //---------------------------------------设施位置信息管理
        private void ShowDevicePositionInf()
        {
            
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from device in Devices.Devices
                         join DevicePosition in Devices.T_DevicePosition on device.DeviceId equals DevicePosition.iDeviceId into MainDevice
                         from RightMain in MainDevice.DefaultIfEmpty()
                         select new
                         {
                             设备编号 = device.DeviceId,
                             /*
                             端口 = device.interfaces,
                             IP地址 = device.ip,*/
                             单位名称 = RightMain == null ? "" : RightMain.sUnitName,
                             楼号 = RightMain == null ? "" : RightMain.sBuildingNo,
                             楼层号 = RightMain == null ? "" : RightMain.iFloor.ToString(),
                             房间号 = RightMain == null ? "" : RightMain.sRoomNo,
                             房内号 = RightMain == null ? "" : RightMain.sNoInRoom,
                             使用者姓名 = RightMain == null ? "" : RightMain.sUserName.Trim(),
                             备注 = RightMain == null ? "" : RightMain.sRemark.Trim()

                         };
            dgv_ShowPosition.DataSource= result;
        }
        private void tc_Main_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex == 1)
            { ShowDeviceBaseInf(); }
        }

        private void tc_DeviceInf_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPageIndex == 1)
            { ShowDevicePositionInf(); }
            else if (e.TabPageIndex == 2)
            { ShowDeviceConfigInf(); }
            else if (e.TabPageIndex == 3)
            { ShowDeviceProviderInf(); }
            else if (e.TabPageIndex == 4)
            { ShowDeviceDriverInf(); }
        }

        private void dgv_ShowPosition_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_ShowPosition.SelectedRows.Count != 0)
            {
                if (e.RowIndex >= 0)
                {
                    tbDeviceID.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[0].Value.ToString();
                    tbUnitName.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[3].Value.ToString();
                    tbBuildingNo.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[4].Value.ToString();
                    tbFloor.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[5].Value.ToString();
                    tbRoomNo.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[6].Value.ToString();
                    tbNoInRoom.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[7].Value.ToString();
                    tbUser.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[8].Value.ToString();
                    tbRemark.Text = dgv_ShowPosition.Rows[e.RowIndex].Cells[9].Value.ToString();
                    if (tbUnitName.Text.ToString() == "")
                    { bnInsertDeviceInf.Enabled = true; }
                    else
                    {
                        bnUpdateDeviceInf.Enabled = true;
                        bnDeleteDeviceInf.Enabled = true;
                    }
                    tbUnitName.Focus();
                }
            }
        }

        private void bnInsertDeviceInf_Click(object sender, EventArgs e)
        {

            
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            T_DevicePosition dPosition = new T_DevicePosition();
            try
            {
                dPosition.iDeviceId = Convert.ToInt32(tbDeviceID.Text.ToString());
                dPosition.sUnitName = tbUnitName.Text.ToString();
                dPosition.sBuildingNo = tbBuildingNo.Text.ToString();
                dPosition.iFloor = Convert.ToInt32(tbFloor.Text.ToString());
                dPosition.sRoomNo = tbRoomNo.Text.ToString();
                dPosition.sNoInRoom = tbNoInRoom.Text.ToString();
                dPosition.sUserName = tbUser.Text.ToString();
                dPosition.sRemark = tbRemark.Text.ToString();
                Devices.T_DevicePosition.InsertOnSubmit(dPosition);
                Devices.SubmitChanges();
                ShowDevicePositionInf();
                MessageBox.Show("添加成功");
                CancelDeviceInf();
               
            }
            catch
            { MessageBox.Show("添加失败！"); }
   
        }

        private void tbUnitName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbBuildingNo.Focus(); }
        }

        private void tbBuildingNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbFloor.Focus(); }
        }

        private void tbFloor_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbRoomNo.Focus(); }
        }

        private void tbRoomNo_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbNoInRoom.Focus(); }
        }

        private void tbNoInRoom_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbUser.Focus(); }
        }

        private void tbUser_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tbRemark.Focus(); }
        }

        private void bnDeleteDeviceInf_Click(object sender, EventArgs e)
        {
            
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var result = from dPosition in Devices.T_DevicePosition
                             where dPosition.iDeviceId == Convert.ToInt32(tbDeviceID.Text.ToString())
                             select dPosition;
                Devices.T_DevicePosition.DeleteAllOnSubmit(result);
                Devices.SubmitChanges();
                MessageBox.Show("删除成功！");
                ShowDevicePositionInf();
                CancelDeviceInf();
            }
            catch
            { MessageBox.Show("删除失败！"); }
 
        }

        private void bnUpdateDeviceInf_Click(object sender, EventArgs e)
        {
            
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var result = from dPosition in Devices.T_DevicePosition
                             where dPosition.iDeviceId == Convert.ToInt32(tbDeviceID.Text.ToString())
                             select dPosition;
                foreach(T_DevicePosition dp in result)
                {
                    dp.sUnitName = tbUnitName.Text.ToString();
                    dp.sBuildingNo = tbBuildingNo.Text.ToString();
                    dp.iFloor = Convert.ToInt32(tbFloor.Text.ToString());
                    dp.sRoomNo = tbRoomNo.Text.ToString();
                    dp.sNoInRoom = tbNoInRoom.Text.ToString();
                    dp.sUserName = tbUser.Text.ToString();
                    dp.sRemark = tbRemark.Text.ToString();
                  Devices.SubmitChanges();
                }
                MessageBox.Show("修改成功！");
                ShowDevicePositionInf();
                CancelDeviceInf();
            }
            catch
            { MessageBox.Show("修改失败！"); }
        }

        private void bnCancelDeviceInf_Click(object sender, EventArgs e)
        {
            CancelDeviceInf();
        }
        private void CancelDeviceInf()
        {
            tbDeviceID.Text = "";
            tbUnitName.Text = "";
            tbBuildingNo.Text = "";
            tbFloor.Text = "";
            tbRoomNo.Text = "";
            tbNoInRoom.Text = "";
            tbUser.Text = "";
            tbRemark.Text = "";
            bnDeleteDeviceInf.Enabled = false;
            bnUpdateDeviceInf.Enabled = false;
            bnInsertDeviceInf.Enabled = false;
        }
//----------------------------------------设施配置管理
        private void cb_DCType_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue ==13)
                tb_DCBrand.Focus();
        }

        private void tb_DCBrand_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
                tb_DCModel.Focus(); 

        }

        private void tb_DCModel_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
                tb_DCCPU.Focus() ;
        }

        private void tb_DCCPU_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
                tb_DCMemory.Focus();
        }

        private void tb_DCMemory_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
                tb_DCMainBoard.Focus(); 
        }

        private void tb_DCMainBoard_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
                tb_DCHarddisk.Focus(); 
        }

        private void tb_DCHarddisk_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyValue == 13)
                tb_DCRemark.Focus();
        }
        private void ShowDeviceConfigInf()
        {

            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from device in Devices.Devices
                         join DeviceConfig in Devices.T_DeviceConfiguration on device.DeviceId equals DeviceConfig.iDeviceId into DeviceC
                         from RightConfig in DeviceC.DefaultIfEmpty()
                         select new
                         {
                             设备编号 = device.DeviceId,
                             /*
                             端口 = device.interfaces,
                             IP地址 = device.ip,*/
                             设备种类 = RightConfig == null ? "" : RightConfig.sType.ToString().Trim(),
                             品牌 = RightConfig == null ? "" : RightConfig.sBrand.ToString().Trim(),
                             型号 = RightConfig == null ? "" : RightConfig.sModel.ToString().Trim(),
                             CPU信息 = RightConfig == null ? "" : RightConfig.sCPU.ToString().Trim(),
                             内存信息 = RightConfig == null ? "" : RightConfig.sMemory.ToString().Trim(),
                             主板信息 = RightConfig == null ? "" : RightConfig.sMainBoard.ToString().Trim(),
                             硬盘信息 = RightConfig == null ? "" : RightConfig.sHarddisk.ToString().Trim(),
                             备注 = RightConfig == null ? "" : RightConfig.sRemark.Trim()

                         };
            dgv_DeviceConfig.DataSource= result;
        }

        private void dgv_DeviceConfig_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_DeviceConfig.SelectedRows.Count != 0)
            {
                if (e.RowIndex >= 0)
                {
                    tb_DConfigID.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[0].Value.ToString();
                    cb_DCType.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[3].Value.ToString();
                    tb_DCBrand.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[4].Value.ToString();
                    tb_DCModel.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[5].Value.ToString();
                    tb_DCCPU.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[6].Value.ToString();
                    tb_DCMemory.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[7].Value.ToString();
                    tb_DCMainBoard.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[8].Value.ToString();
                    tb_DCHarddisk.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[9].Value.ToString();
                    tb_DCRemark.Text = dgv_DeviceConfig.Rows[e.RowIndex].Cells[10].Value.ToString();
                    if (tb_DCBrand.Text.ToString() == "")
                    { bnDCInsert.Enabled = true; }
                    else
                    {
                        bnDCUpdate.Enabled = true;
                        bnDCDelete.Enabled = true; 
                        
                    }
                    cb_DCType.Focus();
                }
            }
        }

        private void bnDCInsert_Click(object sender, EventArgs e)
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            T_DeviceConfiguration  dConfiguration = new T_DeviceConfiguration();
            try
            {
                dConfiguration.iDeviceId = Convert.ToInt32(tb_DConfigID.Text.ToString().Trim());
                dConfiguration.sType = cb_DCType.Text.ToString();
                dConfiguration.sBrand = tb_DCBrand.Text.ToString().Trim();
                dConfiguration.sModel = tb_DCModel.Text.ToString().Trim();
                dConfiguration.sCPU = tb_DCCPU.Text.ToString().Trim();
                dConfiguration.sMemory = tb_DCMemory.Text.ToString().Trim();
                dConfiguration.sMainBoard = tb_DCMainBoard.Text.ToString().Trim();
                dConfiguration.sHarddisk = tb_DCHarddisk.Text.ToString().Trim();
                dConfiguration.iID = 0;
                dConfiguration.sRemark = tb_DCRemark.Text.ToString().Trim();

                Devices.T_DeviceConfiguration.InsertOnSubmit(dConfiguration);
                Devices.SubmitChanges();
                ShowDeviceConfigInf();
                MessageBox.Show("添加成功");
                CancelDCCancel();

            }
            catch
            { MessageBox.Show("添加失败！"); }
        }
        private void CancelDCCancel()
        {
            tb_DConfigID.Text = "";
            tb_DCBrand.Text = "";
            tb_DCModel.Text = "";
            tb_DCCPU.Text = "";
            tb_DCMemory.Text = "";
            tb_DCMainBoard.Text = "";
            tb_DCHarddisk.Text = "";
            tb_DCRemark.Text = "";
            bnDCDelete.Enabled = false;
            bnDCInsert.Enabled = false;
            bnDCUpdate.Enabled = false; 

        }

        private void bnDCUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var result = from dConfig in Devices.T_DeviceConfiguration
                             where dConfig.iDeviceId == Convert.ToInt32(tb_DConfigID.Text.ToString().Trim())
                             select dConfig;
                foreach (T_DeviceConfiguration  dc in result)
                {
                    dc.sType = cb_DCType.Text.ToString().Trim();
                    dc.sBrand = tb_DCBrand.Text.ToString().Trim();
                    dc.sModel = tb_DCModel.Text.ToString().Trim();
                    dc.sCPU = tb_DCCPU.Text.ToString().Trim();
                    dc.sMemory = tb_DCMemory.Text.ToString().Trim();
                    dc.sMainBoard = tb_DCMainBoard.Text.ToString().Trim();
                    dc.sHarddisk = tb_DCHarddisk.Text.ToString().Trim();
                    dc.sRemark = tb_DCRemark.Text.ToString().Trim(); 
                    Devices.SubmitChanges();
                }
                MessageBox.Show("修改成功！");
                ShowDeviceConfigInf();
                CancelDCCancel();
            }
            catch
            { MessageBox.Show("修改失败！"); }
        }

        private void bnDCCancel_Click(object sender, EventArgs e)
        {
            CancelDCCancel();
        }

        private void bnDCDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var result = from dConfig in Devices.T_DeviceConfiguration 
                             where dConfig.iDeviceId == Convert.ToInt32(tb_DConfigID.Text.ToString().Trim())
                             select dConfig;
                Devices.T_DeviceConfiguration.DeleteAllOnSubmit(result);
                Devices.SubmitChanges();
                MessageBox.Show("删除成功！");
                ShowDeviceConfigInf();
                CancelDCCancel();
            }
            catch
            { MessageBox.Show("删除失败！"); }
 
        }
        //-----------------------------------------------设备供货商管理
        private void ShowDeviceProviderInf()
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from Provider in Devices.T_DeviceProvider  orderby Provider.iID descending 
                         select new
                         {
                             采购编号 = Provider.iID,
                             供货商名称 = Provider.sProviderName.ToString().Trim(),
                             联系地址 = Provider.sAddress.ToString().Trim(),
                             联系电话 = Provider.sCall.ToString().Trim(),
                             联系人 = Provider.sName.ToString().Trim(),
                             采购时间 = Provider.tInDate.ToLongDateString(),
                             基本保质期 = Provider.iGuaranteePeriod,
                             金额 = Provider.cPrice,
                             其他约定 = Provider.sOther.ToString().Trim()
                            

                         };
             dgv_DProvider.DataSource = result;
        }
        private void CancelDProvider()
        {
            tb_DPID.Text = "";
            tb_DProvider.Text = "";
            tb_DPAddress.Text = "";
            tb_DPCall.Text = "";
            tb_DPName.Text = "";
            tb_DPGuaranteePeriod.Text = "";
            tb_DPMoney.Text = "";
            tb_DPOther.Text = "";
            bnDPDelete.Enabled = false;
            bnDPInsert.Enabled = true;
            bnDPUpdate.Enabled = false;


        }
        private void bnDPInsert_Click(object sender, EventArgs e)
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            T_DeviceProvider  dProvider = new T_DeviceProvider();
            try
            {
                dProvider.iID= Convert.ToInt32(tb_DPID.Text.ToString().Trim());
                dProvider.sProviderName = tb_DProvider.Text.ToString().Trim();
                dProvider.sAddress = tb_DPAddress.Text.ToString().Trim();
                dProvider.sCall = tb_DPCall.Text.ToString().Trim();
                dProvider.sName = tb_DPName.Text.ToString().Trim();
                dProvider.tInDate = dtp_DPInDate.Value.Date;
                dProvider.iGuaranteePeriod =Convert.ToInt32(tb_DPGuaranteePeriod.Text.ToString().Trim());
                dProvider.cPrice = Convert.ToDecimal(tb_DPMoney.Text.ToString().Trim());
                dProvider.sOther = tb_DPOther.Text.ToString().Trim();

                Devices.T_DeviceProvider.InsertOnSubmit(dProvider);
                Devices.SubmitChanges();
                ShowDeviceProviderInf();
                MessageBox.Show("添加成功");
                CancelDProvider();

            }
            catch
            { MessageBox.Show("添加失败！"); }
        }

        private void bnDPUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var result = from dProvider in Devices.T_DeviceProvider
                             where dProvider.iID== Convert.ToInt32(tb_DPID.Text.ToString().Trim())
                             select dProvider;
                foreach (T_DeviceProvider  dp in result)
                {
                    dp.sProviderName = tb_DProvider.Text.ToString().Trim();
                    dp.sAddress = tb_DPAddress.Text.ToString().Trim();
                    dp.sCall = tb_DPCall.Text.ToString().Trim();
                    dp.sName = tb_DPName.Text.ToString().Trim();
                    dp.tInDate = dtp_DPInDate.Value.Date;
                    dp.iGuaranteePeriod = Convert.ToInt32(tb_DPGuaranteePeriod.Text.ToString().Trim());
                    dp.cPrice = Convert.ToDecimal(tb_DPMoney.Text.ToString().Trim());
                    dp.sOther = tb_DPOther.Text.ToString().Trim();
                    Devices.SubmitChanges();
                }
                MessageBox.Show("修改成功！");
                ShowDeviceProviderInf();
                CancelDProvider();
            }
            catch
            { MessageBox.Show("修改失败！"); }
        }

        private void bnDPDelete_Click(object sender, EventArgs e)
        {
            try
            {
                DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
                var FindResult = from dConfig in Devices.T_DeviceConfiguration
                                 where dConfig.iID == Convert.ToInt32(tb_DPID.Text.ToString().Trim())
                                 select dConfig;
                if (FindResult.Count() == 0)
                {

                    var result = from dProvider in Devices.T_DeviceProvider
                                 where dProvider.iID == Convert.ToInt32(tb_DPID.Text.ToString().Trim())
                                 select dProvider;
                    Devices.T_DeviceProvider.DeleteAllOnSubmit(result);
                    Devices.SubmitChanges();
                    MessageBox.Show("删除成功！");
                    ShowDeviceProviderInf();
                    CancelDProvider();
                }
                else
                { MessageBox.Show("该供货商已经有设备明细，不能删除！"); }
            }
            catch
            { MessageBox.Show("删除失败！"); }
 
        }

        private void bnDPCancel_Click(object sender, EventArgs e)
        {
            CancelDProvider();
        }

        private void tb_DPID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DProvider.Focus(); }
        }

        private void tb_DProvider_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPAddress.Focus(); }
        }

        private void tb_DPAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPCall.Focus(); }
        }

        private void tb_DPCall_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPName.Focus(); }
        }

        private void tb_DPName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { dtp_DPInDate.Focus(); }
        }

        private void dtp_DPInDate_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPGuaranteePeriod.Focus(); }
        }

        private void tb_DPGuaranteePeriod_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPMoney.Focus(); }
        }

        private void tb_DPMoney_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            { tb_DPOther.Focus(); }
        }

        private void dgv_DProvider_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgv_DProvider.SelectedRows.Count != 0)
            {
                if (e.RowIndex >= 0)
                {
                    tb_DPID.Text = dgv_DProvider.Rows[e.RowIndex].Cells[0].Value.ToString();
                    tb_DProvider.Text = dgv_DProvider.Rows[e.RowIndex].Cells[1].Value.ToString();
                    tb_DPAddress.Text = dgv_DProvider.Rows[e.RowIndex].Cells[2].Value.ToString();
                    tb_DPCall.Text = dgv_DProvider.Rows[e.RowIndex].Cells[3].Value.ToString();
                    tb_DPName.Text = dgv_DProvider.Rows[e.RowIndex].Cells[4].Value.ToString();
                    dtp_DPInDate.Value = Convert.ToDateTime(dgv_DProvider.Rows[e.RowIndex].Cells[5].Value.ToString()); 
                    tb_DPGuaranteePeriod.Text =dgv_DProvider.Rows[e.RowIndex].Cells[6].Value.ToString();
                    tb_DPMoney.Text = dgv_DProvider.Rows[e.RowIndex].Cells[7].Value.ToString();
                    tb_DPOther.Text = dgv_DProvider.Rows[e.RowIndex].Cells[8].Value.ToString();
               
                    bnDPDelete.Enabled = true;
                    bnDPUpdate.Enabled = true;
                    bnDPInsert.Enabled=false;

                    }
                    tb_DPID.Focus();
                }
            }

        private void bnDPDetailFind_Click(object sender, EventArgs e)
        {
            if (tb_DPID.Text.ToString().Trim() != "")
            {  frmShowProviderDetail pd = new frmShowProviderDetail();
               pd.id = tb_DPID.Text.ToString().Trim();
               pd.ProviderName = tb_DProvider.Text.ToString().Trim();
               pd.Indate = dtp_DPInDate.Value.Date.ToLongDateString(); 
               pd.ShowDialog();
               
   
            }
            else
            { MessageBox.Show("请先选择供货商，再找明细！"); }
        }
        //-----------------------------------------------------------设备驱动管理
        private void ShowDeviceDriverInf()
        {
            DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            var result = from device in Devices.Devices
                         join DeviceDriver in Devices.T_DeviceDriver on device.DeviceId equals DeviceDriver.iDeviceId into DDG
                         from RightMain in DDG.DefaultIfEmpty()
                         select new
                         {
                             设备编号 = device.DeviceId,
                             /*
                             端口 = device.interfaces,
                             IP地址 = device.ip,*/
                             存放设备 = RightMain == null ? "" : RightMain.sComputer,
                             存放路径= RightMain == null ? "" : RightMain.sFilePath.ToString().Trim(),
                             设备名称 = RightMain == null ? "" : RightMain.sDeviceName.ToString().Trim(),
                             安装备注 = RightMain == null ? "" : RightMain.sRemark.ToString().Trim()
                            

                         };
            dgv_DDShow.DataSource = result;
        }
        private void CancelDeviceDriver()
        {
            tb_DDDeviceID.Text = "";
            tb_DDComputer.Text = "";
            tb_DDFilePath.Text = "";
            tb_DDName.Text = "";
            tb_DDRemark.Text = "";
            bnDDInsert.Enabled = false;
            bnDDUpdate.Enabled = false;
            bnDDDelete.Enabled = false; 
        }
        private void bnDDInsert_Click(object sender, EventArgs e)
        {
           /* DataDeviceDataContext Devices = new DataDeviceDataContext(Conn);
            T_DeviceDriver  dDriver = new T_DeviceDriver();
            try
            {
                dDriver.iDeviceId = Convert.ToInt32(tb_DDDeviceID.Text.ToString().Trim());
                dDriver.sComputer = tb_DDComputer.Text.ToString().Trim();
                dDriver.sFilePath = tb_DDFilePath.Text.ToString().Trim();
                dDriver.sDeviceName = tb_DDName.Text.ToString().Trim();
                dDriver.sRemark = tb_DDRemark.Text.ToString().Trim();

                Devices.T_DeviceProvider.InsertOnSubmit(dDriver);
                Devices.SubmitChanges();
                ShowDeviceProviderInf();
                MessageBox.Show("添加成功");
                CancelDProvider();

            }
            catch
            { MessageBox.Show("添加失败！"); }
            */
        }
       
        
    }
}